package com.february.todo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.facebook.stetho.Stetho;
import com.february.leftslidelib.CustomLinearLayout;
import com.february.leftslidelib.DPIUtil;
import com.february.todo.DataBase.DataBaseOperate;
import com.february.todo.MyCalendar.SimpleTodoListAdapter;
import com.february.todo.tools.Bean_todo;
import com.february.todo.tools.NavigateBar;

import java.util.List;

public class TodoListActivity extends AppCompatActivity {
    private TodoListAdapter mTodoListAdapter;
    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_todo_list);
        DPIUtil.setDensity(getResources().getDisplayMetrics().density);
        Stetho.initializeWithDefaults(this);



        mRecyclerView = findViewById(R.id.recyclerView);
        View root = findViewById(R.id.root);
        if (root instanceof CustomLinearLayout) {
            CustomLinearLayout cll = (CustomLinearLayout) root;
            cll.setOnTouchListener(new CustomLinearLayout.OnTouchListener() {
                @Override
                public void doTouch(Point point) {
                    if (mTodoListAdapter != null) {
                        mTodoListAdapter.restoreItemView(point);
                    }
                }
            });
        }

        DataBaseOperate DBoprate=new DataBaseOperate(this);
        List<Bean_todo> todoList = DBoprate.getAllTodo();

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mTodoListAdapter = new TodoListAdapter(this, this, mRecyclerView, todoList);
        mTodoListAdapter.setOnItemLongClickListener(new SimpleTodoListAdapter.RecyclerViewOnItemLongClickListener() {
            @Override
            public boolean onItemLongClickListener(View view, int position) {
                Intent intent= new Intent(TodoListActivity.this, AddTodoActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("bean_todo",todoList.get(position));
                intent.putExtras(bundle);
                startActivityForResult(intent,4);
                return true;
            }
        });
        mRecyclerView.setAdapter(mTodoListAdapter);

        NavigateBar navigateBar=findViewById(R.id.navigateBar);
        navigateBar.setContext(TodoListActivity.this);
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==4){
            if (resultCode == RESULT_OK) {
                Bean_todo bean_todo = (Bean_todo) data.getSerializableExtra("bean_todo");
                DataBaseOperate DBoprate=new DataBaseOperate(TodoListActivity.this);
                DBoprate.updateTodo(bean_todo);
                List<Bean_todo> todoList = DBoprate.getAllTodo();
                mTodoListAdapter = new TodoListAdapter(this, this, mRecyclerView, todoList);
                mTodoListAdapter.setOnItemLongClickListener(new SimpleTodoListAdapter.RecyclerViewOnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClickListener(View view, int position) {
                        Intent intent= new Intent(TodoListActivity.this, AddTodoActivity.class);
                        Bundle bundle=new Bundle();
                        bundle.putSerializable("bean_todo",todoList.get(position));
                        intent.putExtras(bundle);
                        startActivityForResult(intent,4);
                        return true;
                    }
                });
                mRecyclerView.setAdapter(mTodoListAdapter);
            }
        }
    }

    public static class MyVH extends RecyclerView.ViewHolder {
        public MyVH(@NonNull View itemView) {
            super(itemView);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            Intent intent=new Intent(TodoListActivity.this, MainActivity.class);
            startActivity(intent);
        }
        return super.onKeyDown(keyCode, event);
    }
}